package com.sk.weichat.ui.mucfile.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.sk.weichat.R;
import com.sk.weichat.helper.AvatarHelper;
import com.sk.weichat.ui.mucfile.bean.FileBean;
import com.sk.weichat.util.GetFileSizeUtil;
import com.sk.weichat.util.TimeUtils;

import java.util.List;

public class FileAdapter extends DelegateAdapter.Adapter {

    private OnFileBeanClickListener listener;

    public interface OnFileBeanClickListener {
        void onClick(FileBean fileBean, int pos);
    }

    public void setOnFileBeanClickListener(OnFileBeanClickListener onclickListener) {
        this.listener = onclickListener;
    }

    public List<FileBean> getData() {
        return fileBeanList;
    }

    private List<FileBean> fileBeanList;
    Context context;

    public FileAdapter(Context context, List<FileBean> fileBeanList) {
        this.context = context;
        this.fileBeanList = fileBeanList;
    }


    @Override
    public LayoutHelper onCreateLayoutHelper() {
        LinearLayoutHelper linearLayoutHelper = new LinearLayoutHelper();
        return linearLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_media_file, null);
        VirtualLayoutManager.LayoutParams lp = new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new FileHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        FileBean filebean = fileBeanList.get(i);
        if (filebean == null||!(viewHolder instanceof FileHolder)) {
            return;
        }
        FileHolder holder = (FileHolder) viewHolder;
        holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener && fileBeanList != null && fileBeanList.size() > i) {
                    listener.onClick(fileBeanList.get(i), i);
                }
            }
        });
//                    //选择
        if (filebean.isChecked()) {
            holder.ivChecked.setImageResource(R.mipmap.icon_checked);
        } else {
            holder.ivChecked.setImageResource(R.mipmap.icon_checkedbox_normal);
        }
        //文件名
        holder.tvFileName.setText(filebean.getFileName());
        //修改时间
        holder.tvTime.setText(TimeUtils.getFriendlyTimeDesc(context, filebean.getFile().lastModified() / 1000));
        //文件大小
        String sizeStr = GetFileSizeUtil.formatFileSize(filebean.getFile().length());
        holder.tvFileSize.setText(sizeStr);

        String fileName = filebean.getFileName();
        String type = "";
        try {
            type = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
        } catch (Exception e) {

        }
        if (type.equals("mp4") || type.equals("avi")) {
            AvatarHelper.getInstance().displayVideoThumb(filebean.getFile().getAbsolutePath(), holder.ivFileType);
        } else {
            //文件图标
            AvatarHelper.getInstance().fillFileView(type, holder.ivFileType);
        }
    }

    @Override
    public int getItemCount() {
        return fileBeanList == null ? 0 : fileBeanList.size();
    }

    class FileHolder extends RecyclerView.ViewHolder {
        ImageView ivChecked;
        ImageView ivFileType;
        TextView tvFileName;
        TextView tvTime;
        TextView tvFileSize;
        ConstraintLayout constraintLayout;

        public FileHolder(@NonNull View itemView) {
            super(itemView);
            ivChecked = itemView.findViewById(R.id.iv_check);
            ivFileType = itemView.findViewById(R.id.iv_file_type);
            tvFileName = itemView.findViewById(R.id.tv_file_name);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvFileSize = itemView.findViewById(R.id.tv_file_size);
            constraintLayout = itemView.findViewById(R.id.content);
        }
    }

}
